package com.seven.ezpermissions

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner

/**
 * Simple and easy permissions request
 * @author seven.xie
 * @date 2020/7/10 14:12
 * email: se7en.xie@qq.com
 */

class EzPermissions private constructor() : LifecycleEventObserver {

    private var currentRequest: PermissionRequest? = null

    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        currentRequest?.onStateChanged(source, event, this) OnDestroyEvent@{
            currentRequest = null
        }
    }

    companion object {
        private val instance: EzPermissions by lazy { EzPermissions() }

        fun request(
            activity: AppCompatActivity,
            permissions: Array<out String>,
            requestCode: Int,
            onGranted: OnGrantedCallback? = null,
            onDenied: OnDeniedCallback? = null
        ) {
            request(
                PermissionRequest.Builder(
                    activity, permissions, requestCode
                ).onGranted(onGranted)
                    .onDenied(onDenied)
                    .build()
            )
        }

        fun request(
            fragment: Fragment,
            permissions: Array<out String>,
            requestCode: Int,
            onGranted: OnGrantedCallback? = null,
            onDenied: OnDeniedCallback? = null
        ) {
            request(
                PermissionRequest.Builder(
                    fragment, permissions, requestCode
                ).onGranted(onGranted)
                    .onDenied(onDenied)
                    .build()
            )
        }

        fun request(request: PermissionRequest) {
            val context = request.helper.context
            context ?: return
            if (hasPermissions(context, request.permissions)) {
                request.onGrantedCallback?.invoke(request.requestCode)
                return
            }

            request.addLifecycleObserver(instance)
            instance.currentRequest = request
            request.request()
        }

        fun onRequestPermissionsResult(
            requestCode: Int,
            permissions: Array<out String>,
            grantResults: IntArray
        ) {
            instance.currentRequest?.onRequestPermissionsResult(
                requestCode,
                permissions,
                grantResults
            )
        }

        private fun hasPermissions(context: Context, permissions: Array<out String>): Boolean {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                return true
            }
            return permissions.all { permission ->
                ContextCompat.checkSelfPermission(
                    context,
                    permission
                ) == PackageManager.PERMISSION_GRANTED
            }
        }
    }
}